var debug = require('debug')('consutant:index_router'),
    wechat = require('wechat'),
    settings = require('../config'),
    utils = require('../utils/wechatutil'),
    models = require("../models"),
    User = models.User,
    _ = require('underscore');
var async = require('async');
module.exports = function (app) {
    app.get("/followers", function (req, res) {
        new utils().getAPI(function (api) {
            var opendidlist;
            async.series([

                function (callback) {
                    api.getFollowers(function (err, result) {
                        if (result) {
                            opendidlist = result.data.openid;
                            debug("this followers is ", opendidlist);
                            callback(null, 1);
                        } else {
                            res.end('opendidList fail,请刷新！！');
                        }

                    })
                },
                function (callback) {
                    async.each(opendidlist, function (element, index, list) {
                        var openid = element;
                        debug('this value is:', element);
                        var wcuser = new User();
                        wcuser.findUserByOpenid(openid, function (err, doc) {
                            if (err) {
                                debug("find user fail", err);
                                res.end("fail");
                            } else {
                                if (doc) {
                                    //wcuser=new User(doc);
                                    api.getWhichGroup(openid, function (err, gpresult) {
                                        if (gpresult) {
                                            debug("ddddddddddddddddddddd", gpresult)
                                            wcuser.user_group = gpresult.groupid;
                                            api.getUser(openid, function (err, uresult) {
                                                if (uresult) {
                                                    wcuser.nickname = uresult.nickname;
                                                    debug("this nickname is ", uresult.nickname);
                                                    var options = {
                                                        user_group: gpresult.groupid,
                                                        nickname: uresult.nickname
                                                    };
                                                    wcuser.updateUserByOpneId(openid, options, function (err) {
                                                        if (err) {
                                                            debug("update user fail", err);
                                                            res.end("fail");
                                                        } else {
                                                            res.end("success");
                                                        }
                                                    })
                                                } else {
                                                    res.end('uresult fail,请刷新！！！');
                                                }

                                            })
                                        } else {
                                            res.end('gpresult fail,请刷新！！');
                                        }

                                    });
                                } else {
                                    wcuser.wcopenid = element;
                                    wcuser.weight = 0;
                                    wcuser.count = 5;
                                    wcuser.random = Math.random();
                                    api.getWhichGroup(openid, function (err, gpresult) {
                                        if (gpresult) {
                                            debug("ddddddddddddddddddddd", gpresult)
                                            wcuser.user_group = gpresult.groupid;
                                            api.getUser(openid, function (err, uresult) {
                                                if (uresult) {
                                                    wcuser.nickname = uresult.nickname;
                                                    debug("this nickname is ", uresult.nickname);
                                                    wcuser.saveUser(function (err) {
                                                        if (err) {
                                                            debug("insert user fail", err);
                                                            res.end("fail");
                                                        } else {
                                                            res.end("success");
                                                        }
                                                    })
                                                } else {
                                                    res.end('uresult fail,请刷新！！');
                                                }

                                            })
                                        } else {
                                            res.end('gpresult fail,请刷新！！');
                                        }

                                    });
                                }
                            }
                        })

                        callback(null, 2);
                    })
                },
                function (callback) {
                    res.end("success");
                }
            ]);

        });
    });
    app.get("/gettop5user", function (req, res) {
        var wcuser = new User();
        wcuser.findTop5User(function (err, result) {
            debug("findTop5User", result);
            res.end("success");
        })
    });
    app.get("/initmenu", function (req, res) {
        var menu = {
            "button": [
                {
                    "name": "热门话题!",
                    "type": "click",
                    "key": "V101_ONLINE"
                    /*"name": "热门话题",
                    "type": "view",
                    "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2Fview%2Fpubchat&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
*/
                },
                /*{
                    "name": "下线休息",
                    "type": "click",
                    "key": "V101_OFFLINE"
                },*/
                {
                    "name": "其他操作",
                    "type": "click",
                    "sub_button": [
                        {
                            "type": "view",
                            "name": "成为咨询师",
                            "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2Fwcpay%2Fbecomedoctor&response_type=code&scope=snsapi_base&state=123#wechat_redirect"

                        },
                        {
                            "type": "view",
                            "name": "问答记录",
                            "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2FgetSessions&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
                        },{
                            "type": "view",
                            "name": "设置选项",
                            "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2Fwcpay%2Fpayoff&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
                        },{
                            "type": "view",
                            "name": "购买点数",
                            "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2Fwcpay%2Fgoodslist&response_type=code&scope=snsapi_base&state=wx631f54c7933d782f#wechat_redirect"
                                //"http://117.78.6.144/wcpay/goodslist"
                        },{
                            "type": "click",
                            "name": "结束会话",
                            "key": "V101_CLOSE_SESSION"//"key": "V100_CLOSE_SESSION"
                        }
                    ]
                }
                 /*{
                    "name": "用户菜单",
                    "sub_button": [
                        {
                            "type": "view",
                            "name": "购买服务",
                            "url": "http://117.78.6.144/wcpay/goodslist"
                        },
                        {
                            "type": "view",
                            "name": "服务记录",
                            "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2FgetSessions&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
                        },
                        {
                            "type": "click",
                            "name": "结束会话",
                            "key": "V101_CLOSE_SESSION"//"key": "V100_CLOSE_SESSION"
                        }
                        *//*,
                        {
                            "type": "view",
                            "name": "成为咨询师",
                            "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2Fwcpay%2Fbecomedoctor&response_type=code&scope=snsapi_base&state=123#wechat_redirect"

                        }*//*
                    ]
                },
                {
                    "name": "咨询菜单",
                    *//*"type": "click",
                    "key": "V101_ONLINE"*//*
                "sub_button": [
                        {
                            "type": "view",
                            "name": "咨询结算",
                            "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx631f54c7933d782f&redirect_uri=http%3A%2F%2F117.78.6.144%2Fwcpay%2Fpayoff&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
                        },
                        {
                            "type": "click",
                            "name": "咨询签到",
                            "key": "V101_ONLINE"
                        },
                        {
                            "type": "click",
                            "name": "咨询签退",
                            "key": "V101_OFFLINE"
                        }
                    ]
                }*/
            ]
        };

        new utils().getAPI(function (api) {
            api.createMenu(menu, function (err, result) {
                if (err) {
                    debug("initmenu fail", err);
                    res.end("fail");
                } else {
                    debug("initmenu success", result);
                    res.end("success");
                }
            });
        });
    })
}